/*
 * hay dos metodos para la base de datos abajo del todo, en el metodo ActionPerformed, estan comentados;
 */

package interfaze;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import data_base.initialLogin;

public class LoginWindow extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;
	JButton btnAceptar;
	JButton btnCrear;
	JButton btnCancelar;
	boolean crear = false;
	Color background = Color.BLACK;

	public LoginWindow() {
		getContentPane().setBackground(background);
		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		JPanel panel_8 = new JPanel();
		panel_8.setBackground(background);
		getContentPane().add(panel_8);

		JLabel lblPlayplayerLogin = new JLabel("PlayPlayer Login");
		lblPlayplayerLogin.setForeground(Color.WHITE);
		panel_8.add(lblPlayplayerLogin);
		// lblPlayplayerLogin.setHorizontalAlignment(SwingConstants.CENTER);

		JLabel lblIcon = new JLabel();
		lblIcon.setIcon(new ImageIcon("login.png"));
		panel_8.add(lblIcon);
		// lblIcon.setHorizontalAlignment(SwingConstants.RIGHT);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(background);
		getContentPane().add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(background);
		panel_1.add(panel_2);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(background);
		FlowLayout flowLayout_1 = (FlowLayout) panel_4.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_4);

		JLabel lblNewLabel_1 = new JLabel("Usuario");
		lblNewLabel_1.setForeground(Color.WHITE);
		panel_4.add(lblNewLabel_1);

		JPanel panel_5 = new JPanel();
		panel_5.setBackground(background);
		FlowLayout flowLayout = (FlowLayout) panel_5.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_5);

		JLabel lblNewLabel = new JLabel("Contrase\u00F1a");
		lblNewLabel.setForeground(Color.WHITE);
		panel_5.add(lblNewLabel);

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(background);
		panel_1.add(panel_3);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));

		JPanel panel_7 = new JPanel();
		panel_7.setBackground(background);
		FlowLayout flowLayout_2 = (FlowLayout) panel_7.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_7);

		textField_1 = new JTextField();
		panel_7.add(textField_1);
		textField_1.setColumns(20);

		JPanel panel_6 = new JPanel();
		panel_6.setBackground(background);
		FlowLayout flowLayout_3 = (FlowLayout) panel_6.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_6);

		textField = new JTextField();
		panel_6.add(textField);
		textField.setColumns(20);

		JPanel panel = new JPanel();
		panel.setBackground(background);
		getContentPane().add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		btnAceptar = new JButton("Aceptar");
		panel.add(btnAceptar);

		btnCrear = new JButton("Crear");
		panel.add(btnCrear);

		btnCancelar = new JButton("Cancelar");
		panel.add(btnCancelar);

		btnAceptar.addActionListener(this);
		btnCrear.addActionListener(this);
		btnCancelar.addActionListener(this);

		this.setSize(450, 200);
		this.setMinimumSize(new Dimension(450, 200));
		this.setResizable(false);
		this.setLocation(500, 200);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnAceptar)) {
			boolean login = false;
			// este metodo devolvera un booleano (true si existe)(false si no
			// existe)
			login = initialLogin.doLogin(textField_1.getText(),
					textField.getText());
			if (login) {
				ListWindow lW = new ListWindow();
				lW.setLocation(this.getLocation());
				lW.setVisible(true);
				this.dispose();
			}
		} else if (e.getSource().equals(btnCancelar)) {
			if (crear) {
				crear = false;
				btnAceptar.setEnabled(true);
			} else {
				this.dispose();
			}
		} else if (e.getSource().equals(btnCrear)) {
			if (crear) {
				initialLogin.createLogin(textField_1.getText(),
						textField.getText());
				crear = false;
				btnAceptar.setEnabled(true);
			} else {
				crear = true;
				btnAceptar.setEnabled(false);
			}
		}
	}
}
